<?php
/**
 * 从gb网站获取品类数据
 * 2018-5-21
 * liugang
 */
namespace app\controllers\crontab;

use yii;
use app\models\marketing\MailGroup;
use app\components\Site;

use app\models\Elog;
use GuzzleHttp\Client;

class GetRgCategoryInfoController extends \app\base\Controller
{
    // 获取gb品类接口头配置信息
    private $getGbCategoryHeader = [
        'dev'   => [
            "domain" => "",
            "method" => "getCategoryListForLinkageSelect",
            "service" => "com.globalegrow.mgoods.spi.inter.ICategoryService",
            "version" => "1.0.0",
            "tokenId" => "487d842de4e1c9b9c99ac868c7af15a4",
            "url" => ""
        ],
        'test'   => [
            "domain" => "",
            "method" => "getCategoryListForLinkageSelect",
            "service" => "com.globalegrow.mgoods.spi.inter.ICategoryService",
            "version" => "1.0.0",
            "tokenId" => "487d842de4e1c9b9c99ac868c7af15a4",
            "url" => ""
        ],
        'product'   => [
            "domain" => "",
            "method" => "getCategoryListForLinkageSelect",
            "service" => "com.globalegrow.mgoods.spi.inter.ICategoryService",
            "version" => "1.0.0",
            "tokenId" => "5dd73f1f0412b1a8f483ed0655b259b4",
            "url" => ""
        ]
    ];
    // 获取gb品类接口地址
    private $gbCategoryApiUrl = [
        'dev'   => 'http://10.40.2.62:2087/gateway/',
        'test'   => 'http://10.40.2.62:2087/gateway/',
        'product'   => 'http://gb-obs-outer.gw-soa.com/gateway/'
    ];



    /*
     * 定时获取并保存gb站点品类数据
     * /crontab/get-gb-category-info/get-gb-category-data?site=marketing_email 每天执行一次
     * @user liugang
     * @date 2018-5-21
     */
    public function actionGetGbCategoryData()
    {

        $categoryList = Yii::$app->db->createCommand("select cat_id as category_id ,cat_name as categoryName ,parent_id,level as category_grade from rosegal_eload_category ")->queryAll();

        // 初始化表结构
        Yii::$app->db->createCommand("DROP TABLE IF EXISTS rosegal_category_data")->execute();
        $res = Yii::$app->db->createCommand("CREATE TABLE `rosegal_category_data` (
                                                `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
                                                `category_grade` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '品类级别',
                                                `category_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '品类id',
                                                `category_name` varchar(64) NOT NULL DEFAULT '' COMMENT '品类名称',
                                                `parent_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '父级品类id',
                                                `min_category_relation` varchar(255) NOT NULL DEFAULT '' COMMENT '最小品类id与所有父级品类id的关系，以，分割',
                                                `is_leaves` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '是否为最小分类',
                                                PRIMARY KEY (`id`),
                                                KEY `category_grade` (`category_grade`) USING BTREE,
                                                KEY `parent_id` (`parent_id`) USING BTREE,
                                                KEY `min_category_relation` (`min_category_relation`) USING BTREE,
                                                KEY `is_leaves` (`is_leaves`) USING BTREE
                                              ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='RG站品类数据表';")->execute();
        $insertArr = [];
        $insertSql = "insert into rosegal_category_data(category_grade,category_id,category_name,parent_id,min_category_relation,is_leaves) values";
        $temp =[];
        $parentTemp = [];
        foreach ($categoryList as $val) {       //重置下标
              $temp[$val['category_id']] = $val;
              if (! empty($val['parent_id'])) {
                  $parentTemp[$val['parent_id']] = $val;
              }
        }
        unset($categoryList);
        $categoryList = $temp;
        foreach ($categoryList as $index => & $val) {
            $val['is_leaves'] =0;
            $val['min_category_relation'] ='';
            if(empty($val['parent_id'])) {
               continue;
            }
            if (! isset($parentTemp[$val['category_id']])) {         //最小分类
                $val['is_leaves'] = 1;
                $val['min_category_relation'] = ",".$val['category_id'].",";
                $this->getMinCategory($categoryList,$val,$val['min_category_relation']);
            }
        }
        unset($val,$index);
        foreach($categoryList as $val) {
            $insertArr[] = "({$val['category_grade']},{$val['category_id']},\"".$val['categoryName']."\",{$val['parent_id']},'{$val['min_category_relation']}',{$val['is_leaves']})";
        }
        if($insertArr) {
            $insertSql .= join(',',$insertArr);
            // 数据写入edm数据表
            Yii::$app->db->createCommand($insertSql)->execute();
        }
    }

    /**
     * 返回上级分类
     */
    public function getMinCategory($temp , $info,&$str)
    {
        if (! empty($info['parent_id'])) {
           $str = ",".$info['parent_id'].$str;
        }
        if ( isset($temp[$info['parent_id']])) {         //最小分类
            $this->getMinCategory($temp,$temp[$info['parent_id']],$str);
        }
        return '';
    }

}